Notification
is a message that doesn’t appear inside the main UI of the app but as an icon
in the notification area, including more details in the notification drawer.
A
notification is a message you can display to the user outside of your
application's normal UI. Notifications
are used to alert users on some events that requires their attention.
Notifications alert the users through various forms:
- Display a status bar icon
- Flash the LED
- Vibrate
- Ringtones
You can also
show Toast notification i.e.
Toast.makeText(getApplicationContext(), "My Message", Toast.LENGTH_LONG).show();
- Create an Android Project
- Add button in activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btnNotif"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show Notification" />
</RelativeLayout>
3. Create another layout for notification view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Your notification test is successfull !"
android:textSize="18sp" />
</LinearLayout>
4. Now add following code in MainActivity.class
package com.example.androidnotification;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
NotificationManager notificationManager;
static int notificationID = 100;
Button btnMeaasge;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMeaasge = (Button) findViewById(R.id.btnNotif);
btnMeaasge.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
displayMyNotification();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if //it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// This method shows notification
public void displayMyNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext());
// set main title
builder.setContentTitle("Demo Test");
// set notification text
builder.setContentText("You have received a message");
// set Sticker for alert message
builder.setTicker("New Message Alert!");
// set icon
builder.setSmallIcon(R.drawable.ic_launcher);
// Use default sound for notification
builder.setDefaults(Notification.DEFAULT_SOUND);
notificationManager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
// A PendingIntent specifies an action to take in the
// future
PendingIntent myIntent = PendingIntent.getActivity(this, 0,new Intent( this, MyNotification.class), 0);
// on notification click open myIntent
builder.setContentIntent(myIntent);
/* Update the existing notification using same notification ID */
notificationManager.notify(notificationID, builder.build());
}
}
5.
Create MyNotification Activity and paste following code
package com.example.androidnotification;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
import android.widget.Toast;
public class MyNotification extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.notif);
// Remove notification after open this activity
NotificationManager notificationManager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
notificationManager.cancel(MainActivity.notificationID);
}
}
Now run your Application

After click on notification

Leave Comment